package com.dz.pojo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dz.database.CrimeBaseHelper;
import com.dz.database.CrimeCursorWrapper;
import com.dz.database.CrimeDbScheme;
import com.dz.database.CrimeDbScheme.CrimeTable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CrimeLab {

    private static CrimeLab sCrimeLab;

//    private List<Crime> mCrimes;

    private Context mContext;
    private SQLiteDatabase mDatabase;

    //ContentValues 负责写入和更新数据库
    private static ContentValues getContentValues(Crime crime){
        ContentValues values = new ContentValues();
        values.put(CrimeTable.Cols.UUID,crime.getId().toString());
        values.put(CrimeTable.Cols.TITLE,crime.getTitle());
        values.put(CrimeTable.Cols.DATE,crime.getDate().getTime());
        //解决为1
        values.put(CrimeTable.Cols.SOLVED,crime.isSolved()?1:0);
        values.put(CrimeTable.Cols.SUSPECT,crime.getSuspect());
        values.put(CrimeTable.Cols.NUMBER,crime.getNumber());
        return values;
    }


    public static CrimeLab get(Context context) {
        if (sCrimeLab == null) {
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }

    //这里使用context的原因是考虑了生命周期，context生命周期贯穿整个应用
    private CrimeLab(Context context) {
        mContext = context.getApplicationContext();
        mDatabase = new CrimeBaseHelper(mContext).getWritableDatabase();
//        mCrimes = new ArrayList<>();
    }

    public List<Crime> getCrimes() {
//        return mCrimes;
        List<Crime> crimes = new ArrayList<>();
        CrimeCursorWrapper cursor = queryCrimes(null, null);

        try{
            //将指针指向第一个元素
            cursor.moveToFirst();
            //cursor.isAfterLast()  没有数据
            while (!cursor.isAfterLast()){
                crimes.add(cursor.getCrime());
                //指向下一个
                cursor.moveToNext();
            }
        }
        finally {
            cursor.close();
        }
        return crimes;
    }

    public Crime getCrime(UUID id) {
//        for (Crime crime : mCrimes) {
//            if (crime.getId().equals(id)) {
//                return crime;
//            }
//        }
//        return null;
        CrimeCursorWrapper cursor = queryCrimes(CrimeTable.Cols.UUID + " = ?", new String[]{id.toString()});
        try{
            if (cursor.getCount()==0){
                return null;
            }
            cursor.moveToFirst();
            return cursor.getCrime();
        }
        finally {
            cursor.close();
        }
    }
    //获取Crime的索引
//    public int getCrimeIndex(Crime crime){
//        return mCrimes.indexOf(crime);
//    }
    public void add(Crime crime){
        ContentValues values = getContentValues(crime);
        //向数据库中插入数据
        mDatabase.insert(CrimeTable.NAME,null,values);
//        mCrimes.add(crime);
    }
    //更新数据
    public void update(Crime crime){
        String id = crime.getId().toString();
        ContentValues values = getContentValues(crime);
        mDatabase.update(CrimeTable.NAME,values,CrimeTable.Cols.UUID+" = ?",new String[]{id});
    }
    //查询数据
    private CrimeCursorWrapper queryCrimes(String whereClause , String[] whereArgs){
        Cursor cursor = mDatabase.query(
                CrimeTable.NAME, //tables
                null,   //columns
                whereClause,     //where
                whereArgs,       //whereArgs
                null,    //groupBy
                null,      //having
                null      //orderBy
        );
//        return cursor;
        return new CrimeCursorWrapper(cursor);
    }

    public void removeCrime(Crime crime) {
//         delete(String table, String whereClause, String[] whereArgs) {
        String id = crime.getId().toString();
        mDatabase.delete(CrimeTable.NAME,CrimeTable.Cols.UUID+"=?",new String[]{id});
//        mCrimes.remove(c);
    }
    //找到保存文件的目录
    public File getPtotoFile(Crime crime){
        File filesDir = mContext.getFilesDir();
        return new File(filesDir,crime.getPhotoFileName());
    }
}
